package leetcode.code1233;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public List<String> removeSubfolders(String[] folder) {
		Arrays.sort(folder, (a, b) -> a.length() - b.length());
		List<String> ans = new ArrayList<>();
		Node root = new Node();
		for (String path : folder) {
			String[] d = path.split("/");
			Node node = root;
			for (int i = 0; i < d.length; i++) {
				node = node.add(d[i]);
				if (node.end) {
					break;
				}
			}
			if (!node.end) {
				node.end = true;
				ans.add(path);
			}
		}
		return ans;
	}

	class Node extends HashMap<String, Node> {
		
		boolean end;// 是否是结尾

		public Node add(String key) {
			return this.computeIfAbsent(key, k -> new Node());
		}
		
	}

	@Override
	public void debug4() {
		this.removeSubfolders(H.str2array2("[\"/a\",\"/a/b\",\"/c/d\",\"/c/d/e\",\"/c/f\"]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		Map<Integer, Integer> map = new HashMap<>();
		map.put(1, 1);
		Integer n = map.computeIfPresent(1, (a, b) -> b + 1);
		System.out.println(map.get(1));
		Integer n1 = map.computeIfAbsent(2, k -> 100);
		System.out.println(n1);
		
		
	}

}
